package hello.app.util;

import android.os.Handler;

/*
 * 好きな処理を入れることが出来るスレッドクラス
 */
public class ThreadUtil extends Thread{

	public interface Runner {
		public void exec(Object ...args);
	}
	private Runner mRunner;
	private Object[] mArgs;
	private boolean mForeground = false;
	private final Handler mHandler = new Handler();

	public ThreadUtil(Runner runner){
		this.mRunner = runner;
		mArgs = new Object[0];
	}

	public ThreadUtil(Runner runner, boolean foreground){
		this.mRunner = runner;
		mArgs = new Object[0];
		mForeground = foreground;
	}

	public void start(Object ...args) {
		this.mArgs = args;
		super.start();
	}

	public void run() {
		if(mForeground){
			mHandler.post(new Runnable() {
				public void run() {
					mRunner.exec(mArgs);
				}
			});
		}else{
			mRunner.exec(mArgs);
		}
	}
}
